const { assert, expect } = require("chai");
const { ethers, deployments, getNamedAccounts } = require("hardhat");
const { mine } = require("@nomicfoundation/hardhat-network-helpers");
const { developmentChains } = require("../../help-hardhat-config")

developmentChains.includes(network.name)
    ? describe.skip
    :
    describe("staging-fundme-contract", async function () {
        // tip: 在开发的时候发现很多函数都需要加await 这是一个要注意的，另一个是(await getNamedAccounts()) 这个要写括号
        let firstAccount;
        let secondAccount;
        let fundmeDeploy;
        let fundMe;
        let mockV3AggregatorDeploy;
        let fundMeSecondAccount;

        // 每个it都会执行
        this.beforeEach(async function () {
            await deployments.fixture(["all"])
            firstAccount = (await getNamedAccounts()).firstAccount
            const fundMeDeployment = await deployments.get("FundMe")
            fundMe = await ethers.getContractAt("FundMe", fundMeDeployment.address)
        });

        // test fund and getFund successfully
        it("fund and getFund successfully",
            async function () {
                await fundMe.fund({ value: ethers.parseEther("0.1") });
                await new Promise(resolve => setTimeout(resolve, 19 * 1000))

                let getFund = await fundMe.getFund();
                let getFundTx = await getFund.wait();
                // 事件 账户和余额
                await expect(getFundTx)
                    .to.emit(fundMe, "FundWithdrawByOwner")
                    .withArgs(secondAccount, ethers.parseEther("1"))
            }
        )
     
    })